package com.angus.modules.content.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.angus.modules.content.domain.*;
import com.angus.modules.content.dto.QueryOrderDto;
import com.angus.modules.content.repository.CourseRepository;
import com.angus.modules.content.repository.SearchContentRepository;
import com.angus.modules.content.service.SearchContentService;
import com.angus.modules.content.vo.SysOrderVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class SearchContentServiceImpl implements SearchContentService {

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private SearchContentRepository searchContentRepository;

    @Autowired
    private  SysOrderServiceImpl sysOrderServiceImpl;

    @Override
    public List<SearchContent> getSearchContentList(String searchWord, Long userId) {
        List<SearchContent> Data = searchContentRepository.getSearchContentList(searchWord);
        if(CollUtil.isNotEmpty(Data)){
            List<String> courseCodeMap = extractDistinct(Data, SearchContent::getCourseCode);
            if (CollUtil.isNotEmpty(courseCodeMap)) {
                // 查询课程是否存在
                List<Course> courseCodeList = courseRepository.list(Wrappers.lambdaQuery(Course.class).in(Course::getCourseCode, courseCodeMap));
                if(CollUtil.isNotEmpty(courseCodeList)){
                    List<SearchContent> resData = new ArrayList<>();
                    // 重组为 Map<String, Course>（courseCode 为键）
                    Map<String, Course> courseMap = courseCodeList.stream()
                            .filter(course -> course != null && course.getCourseCode() != null)
                            .collect(Collectors.toMap(
                                    Course::getCourseCode,
                                    Function.identity(),
                                    (existing, replacement) -> existing
                            ));
                    for (SearchContent searchContent : Data) {
                        String courseName = Optional.ofNullable(courseMap.get(searchContent.getCourseCode()))
                                .map(Course::getCourseName)
                                .orElse("未知课程");
                        String contentType = Optional.ofNullable(courseMap.get(searchContent.getCourseCode()))
                                .map(Course::getContentType)
                                .orElse("all");

                        Integer isBuy = 0;
                        // 获取订单是否支付
                        if (userId != null){
                            QueryOrderDto dto = new QueryOrderDto();
                            dto.setStatus(2);
                            dto.setPayMethod(0);
                            dto.setCourseCode(searchContent.getCourseCode());
                            List<SysOrderVo> orderList = sysOrderServiceImpl.getOrderList(dto, userId).getData();
                            if (CollUtil.isNotEmpty(orderList)) {
                                isBuy = 1;
                            }
                        }
                        searchContent.setIsBuy(isBuy);
                        searchContent.setCourseName(courseName);
                        searchContent.setContentType(contentType);
                        resData.add(searchContent);
                    }
                    return resData;
                }
                return Data;
            }
            return Data;
        }
        return Data;
    }

    public static <T, R> List<R> extractDistinct(List<T> list, Function<T, R> mapper) {
        return list.stream()
                .map(mapper)
                .distinct()
                .collect(Collectors.toList());
    }
}
